package coolq.yaozhenyong.custom;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;


public class TimeUtils {
	/**
	 * 获取当前系统时间
	 * 
	 * @return
	 */
	public static Date getNowTime() {
		LocalDate date = LocalDate.now();
		Date date1 = Date.valueOf(date);
		return date1;
	}

	/**
	 * 
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		java.util.Date time = new java.util.Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(time);
		return dateString;
	}

	/**
	 * 
	 * 获取时间
	 * 
	 * @param time 填入格式：HH:mm:ss
	 * @return返回date HH:mm:ss
	 */
	public static java.util.Date getTime(String time) {
		DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		java.util.Date date = null;
		try {
			date = formatter.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy-MM-dd
	 */
	public static String getStringTime() {
		java.util.Date time = new java.util.Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(time);
		return dateString;
	}

	/**
	 * 
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 HH:mm:ss
	 */
	public static String getTime() {
		java.util.Date time = new java.util.Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String dateString = formatter.format(time);
		return dateString;
	}

	/**
	 * 获取当前系统时间一年后的时间
	 * 
	 * @return
	 */
	public static Date getDeadline() {
		LocalDate date = LocalDate.now();
		LocalDate date1 = date.plusYears(1);
		Date date2 = Date.valueOf(date1);
		return date2;
	}

	/***
	 * 判断时间是否为之后时间 与现在时间比较
	 * 
	 * @param time 被比较时间
	 * @return
	 */

	public static boolean AfterTime(Date time) {
		LocalDate date = LocalDate.now();
		Date date1 = Date.valueOf(date);
		if (time.after(date1)) {
			return true;
		} else {

			return false;
		}
	}

	/***
	 * 判断时间是否为之前时间 与现在时间比较
	 * 
	 * @param time 被比较时间
	 * @return
	 */

	public static boolean BeforeTime(Date time) {
		LocalDate date = LocalDate.now();
		Date date1 = Date.valueOf(date);
		if (time.before(date1)) {
			return true;
		} else {

			return false;
		}
	}

	/***
	 * 
	 * @param time 时间String
	 * @return 星期 String
	 */
	public static String WeekDay(String time) {
		try {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date date = format.parse(time);
			Instant instant = date.toInstant();
			ZoneId zoneId = ZoneId.systemDefault();
			LocalDate localDate = instant.atZone(zoneId).toLocalDate();
			DayOfWeek day = localDate.getDayOfWeek();
			String WeekDay = "";

			switch (day) {

			case MONDAY:

				WeekDay = "星期一";

				break;

			case FRIDAY:

				WeekDay = "星期五";

				break;

			case SATURDAY:

				WeekDay = "星期六";

				break;

			case SUNDAY:

				WeekDay = "星期日";

				break;

			case THURSDAY:

				WeekDay = "星期四";

				break;

			case TUESDAY:

				WeekDay = "星期二";

				break;

			case WEDNESDAY:

				WeekDay = "星期三";

				break;

			}

			return WeekDay;
		} catch (ParseException e) {
			return "报错了";
		}
	}

	/**
	 * 判断节次
	 * 
	 * @param time当前时间 HH:mm:ss
	 * @return节次
	 */

	public static String getJc(String time) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			java.util.Date date = formatter.parse(time);
			if (date.after(getTime("08:00:00")) && date.before(getTime("09:40:00"))) {
				return "1-2";
			} else if (date.after(getTime("10:00:00")) && date.before(getTime("11:40:00"))) {
				return "3-4";
			} else if (date.after(getTime("14:00:00")) && date.before(getTime("15:35:00"))) {
				return "5-6";
			} else if (date.after(getTime("15:55:00")) && date.before(getTime("17:30:00"))) {
				return "7-8";
			} else if (date.after(getTime("19:00:00")) && date.before(getTime("20:35:00"))) {
				return "9-10";
			} else {
				return null;
			}

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 根据当前年份判断春、秋季学期
	 * @return
	 */
	public static String getxq() {
		try {
			LocalDate date = LocalDate.now();
			Integer year= date.getYear();
			String year1= String.valueOf(year);
			Integer month= date.getMonthValue();
			if(month<6) {
				return year1+"年春季学期";
			}else {
				return year1+"年秋季学期";
			}
			
		} catch (Exception e) {
			System.out.println("报错了");
			return null;
		}
	}
	/****
	 * 判读时间段为什么时候
	 * 返回 上午 下午 晚上
	 * @param time 起始时间
	 * @param endtime	结束时间
	 */
	public static String getDay(String time, String endtime) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		try {
			java.util.Date datetime = formatter.parse(time);
			java.util.Date dateendtime = formatter.parse(endtime);
			if (datetime.after(getTime("05:59:59")) && datetime.before(getTime("12:00:01")) && dateendtime.after(getTime("05:59:59")) && dateendtime.before(getTime("12:00:01"))) {
				return "上午";
			}else if(datetime.after(getTime("11:59:59")) && datetime.before(getTime("18:00:01")) && dateendtime.after(getTime("11:59:59")) && dateendtime.before(getTime("18:00:01"))) {
				return "下午";
			}else if((datetime.after(getTime("19:59:59")) && datetime.before(getTime("23:59:59")) && dateendtime.after(getTime("19:59:59")) && dateendtime.before(getTime("23:59:59")))) {
				return "晚上";
			}else {
				return "全天";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "全天";
	}
	/***
	 * 判断时间返回Integer
	 *  是否评价 评价以后改变状态 1.未评价 3.待评价 4. 已过期
	 * @param date 
	 */
	public static Integer Comparisontime(java.util.Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date startdate = date;
		
		String stringdate = getStringDate();
		java.util.Date time;
		try {
			time = sdf.parse(stringdate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			startdate = cal.getTime();
			cal.add(Calendar.DAY_OF_MONTH, 2);
			java.util.Date enddate = cal.getTime();
			
			if(time.after(startdate)&&time.before(enddate)) {
				return 1;
			}else if(enddate.after(time)){
				return 3;
			}else if(startdate.before(time)) {
				return 4;
			}
			return 0;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	/****
	 * 根据时间对比 将这个班的数据进行排序
	 * 用于优化考虑
	 * 
	 *  1.未评价 3.待评价 4. 已过期
	 * @param finishttime
	 * @param rulesort 
	 */
	public static int rulesort(java.util.Date date, int rulesort) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date startdate = date;
		
		String stringdate = getStringDate();
		java.util.Date time;
		try {
			time = sdf.parse(stringdate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			startdate = cal.getTime();
			cal.add(Calendar.DAY_OF_MONTH, 2);
			java.util.Date enddate = cal.getTime();
			if(time.after(startdate)&&time.before(enddate)) {
				return 1;
			}else if(enddate.after(time)){
				return rulesort+5000;
			}else if(startdate.before(time)) {
				return rulesort+50000;
			}
			
			return 0;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
